package com.qf.controller;

import com.qf.entity.Details;
import com.qf.service.DetailsService;
import com.qf.service.impl.DetailsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
杨威
2021/9/25
14:59
*/
@WebServlet("/modify")
public class ModifyController extends HttpServlet {
    DetailsService detailsService=new DetailsServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String addrName = request.getParameter("addrName");
        Integer companyId = Integer.valueOf(request.getParameter("companyId"));
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String dateStr = request.getParameter("date");
        Integer state = Integer.valueOf(request.getParameter("state"));
        Date date=null;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Details details=new Details(0,addrName,phone,address,date,state,null);
        if(id==""){//新增
            int i = detailsService.addDetails(details, companyId);
            response.sendRedirect(request.getContextPath()+"/showAll");
        }else {//修改
            details.setId(Integer.valueOf(id));
            detailsService.updateDetails(details,companyId);
            response.sendRedirect(request.getContextPath()+"/showAll");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
